import 'package:flutter/material.dart';

class TstExpansionPanelList extends StatefulWidget {
  @override
  _TstExpansionPanelListSate createState() => _TstExpansionPanelListSate();
}

class _TstExpansionPanelListSate extends State<TstExpansionPanelList> {
  List<int> mList;

  // 存放状态和索引List
  List<ExpandStateBean> expandStateList;

  _TstExpansionPanelListSate() {
    mList = new List();
    expandStateList = new List();
    for (int i = 0; i < 30; i++) {
      mList.add(i);
      // 第一个是索引，第二个是否打开;
      expandStateList.add(ExpandStateBean(i, false));
    }
  }

  // 判断用户点击是否打开
  _setCurrentIndex(int index, isExpand) {
    setState(() {
      // 循环判断用户点击和索引是否一致，并操作状态

      expandStateList.forEach((item) {
        if (item.index == index) {
          item.isOpen = !isExpand;
        }
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('分组组件')),
      // 可滚动的控件
      body: SingleChildScrollView(
        // ExpansionPanelList 必须放在可滚动的控件里面
        child: ExpansionPanelList(
          expansionCallback: (index, bol) {
            _setCurrentIndex(index, bol);
          },
          children: mList.map((index) {
            return ExpansionPanel(
                // 上下文 是否是打开的
                headerBuilder: (context, isExpanded) {
                  return ListTile(
                    title: Text('我是标题.$index'),
                  );
                },
                body: Column(
                  children: <Widget>[
                    ListTile(
                      title: Text('内容1.$index'),
                    ),
                    ListTile(
                      title: Text('内容2.$index'),
                    ),
                    ListTile(
                      title: Text('内容3.$index'),
                    ),
                  ],
                ),

                // 判断是否打开
                isExpanded: expandStateList[index].isOpen);
          }).toList(),
        ),
      ),
    );
  }
}

// 控制打开和关闭的类
class ExpandStateBean {
  var isOpen;
  var index;

  ExpandStateBean(this.index, this.isOpen);
}
